<template>
<div class="comment_placeholder">
  <div 
    class="comment_cover"
     v-if="isActive"
    v-show="isActive=false"
  ></div>
  <div class="comment">
    <!-- 形态1. 简单的展示 -->
    <div
      class="type1"
      v-show="!isActive"
    >
      <input
        type="text"
        placeholder="写跟帖"
        @focus="inputFn"
        v-model.trim="content"
      >
      <router-link :to="`/comment/${article.id}`" class="icon-group">
        <i class="iconfont iconpinglun-"></i>
        <em>{{article.comment_length}}</em>
      </router-link>
      <span class="icon-group " @click="collectFn">
        <van-icon  name="like"  :class="{star: article.has_star}"/>
      </span>
      <span class="icon-group">
        <i class="iconfont iconfenxiang"></i>
      </span>
    </div>
    <!-- 形态2. 文本域，用户可以输入 -->
    <div
      class="type2"
      v-show="isActive"
    >
      <!-- Vant 也有组件 -->
      <textarea
        class="textarea"
        placeholder="回复: @火星网友"
        v-focus
        v-model.trim="content"
      ></textarea>
      <div class="btn-group">
        <span class="btn" @click="isActive = false">取消</span>
        <span class="btn" @click="sendBtn">发送</span>
      </div>
    </div>
  </div>
  </div>

</template>

<script>
// 收藏文章接口
import {updatePostStarByIdAPI,pulishPostCommentsIdAPI} from '../apis/news'
export default {
  props:{
    article: Object,
    commentObj:Object
  },
  data() {
    return {
      isActive: false,
      content:"",
    }
  },
  methods:{
    // 发送点击事件回复评论当前的文章
    async sendBtn() {
     const res = await  pulishPostCommentsIdAPI(this.article.id,{
       content:this.content,
      //  表示
      //  parent_id: this.commentObj && this.commentObj.parent_id
       parent_id: this.commentObj ? this.commentObj.id : null
     });
    //  成功提醒
     this.$toast.success(res.data.message)
    //  清空输入框
     this.content="";
    //  回到形态1
     this.isActive = false;
    //  由于评论列表的数据父组件上，子传父
    // 更新
    this.$emit("update");
    },
    // 输入框获取焦点的时候变成文本域
    inputFn() {
      this.isActive = true
       // 需要等待视图更新后，才能获取到焦点
      // this.$nextTick(() => {
      //   this.$refs.textarea.focus();
      // });
    },
    async collectFn() {
      console.log('实现文章收藏');
      const res = await  updatePostStarByIdAPI(this.article.id)
      console.log(res);
      const {message} = res.data;

      if(message === "收藏成功") {
        this.$toast.success(message)
      } else {
        this.$toast.fail(message)
      }
      this.article.has_star =  !this.article.has_star;
    }
  }
};
</script>

<style lang="less" scoped>
.comment_placeholder{
    height:100px;
}
.comment_cover{
  background-color: rgba(0, 0, 0, .5);
  display: flex;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
}
.comment {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #f2f2f2;
}

.type1 {
  display: flex;
  height: 50px;
  align-items: center;
  padding: 0 10px;
  .star{
    color: red;
}
  .van-icon{
      font-size: 20px;
  }
  input {
    flex: 1;
    height: 30px;
    border-radius: 15px;
    background-color: #d7d7d7;
    border: none;
    padding: 0 10px;
    margin: 10px;
  }
  .icon-group {
    width: 40px;
    height: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    .iconfont {
      font-size: 20px;
    }
    em {
      background-color: red;
      color: #fff;
      position: absolute;
      right: 0px;
      top: 0;
      font-size: 12px;
      border-radius: 15px;
      padding: 0 2px;
    }
  }
}
.type2 {
  display: flex;
  padding: 10px;
  .textarea {
    height: 80px;
    background-color: #d7d7d7;
    border-radius: 5px;
    flex: 1;
    padding: 5px;
  }

  .btn-group {
    margin-left: 10px;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .btn {
      width: 60px;
      height: 25px;
      background-color: red;
      color: #fff;
      border-radius: 100px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}

</style>